package gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import db.domain.SystemPictur;
import db.sqldao.SystemPicturSqlDao;

/**
 * 系统图片输入界面
 * 
 * @author 冀鲁
 * @date 2011-06-28
 * @version 1.0.0.0
 */

@SuppressWarnings("serial")
public class SystemPicturInputForm extends JDialog implements ActionListener,
		MouseListener {
	private int num_x = 5;
	private int num_y = 4;
	private int num = num_x * num_y;
	private JButton btn[] = new JButton[num];
	private String type;
	private SystemPictur systemPictur = new SystemPictur();
	private ShowPicturForm showPictur;

	public SystemPicturInputForm(String type) {
		int x0, y0, width, height, btnWidth, btnHeight;
		int Num = 0;
		btnWidth = 70;
		btnHeight = 70;
		x0 = 0;
		y0 = 0;
		width = btnWidth * 5;
		height = btnHeight * 4 + 30;
		String name = null;
		this.type = type;
		this.setTitle("皮肤管理界面");
		this.setLayout(null);
		Font font = new Font("黑体", Font.BOLD, 25);

		for (int i = 0; i < num_x; i++) {
			for (int j = 0; j < num_y; j++) {
				name = find(Num).getName();
				btn[Num] = new JButton(name);
				btn[Num].setBounds(x0 + i * btnWidth, y0 + j * btnHeight,
						btnWidth, btnHeight);
				btn[Num].addActionListener(this);
				btn[Num].addMouseListener(this);
				this.add(btn[Num]);
				Num++;
			}
		}

		// this.setModal(true);// 窗口独占模式
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		// 匿名内部类
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				dispose();
			}
		});

		// 设置窗口的大小
		this.setSize(width, height);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		this.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		for (int i = 0; i < num; i++) {
			if (e.getSource() == btn[i]) {
				showPictur.dispose();
				new SystemPicturManager(systemPictur);

			}
		}
	}

	/*
	 * 查找组号为no的皮肤组 并返回
	 */
	SystemPictur find(int no) {
		SystemPicturSqlDao sDao = new SystemPicturSqlDao();
		ArrayList<SystemPictur> al = sDao.getSystemPictur("where no="
				+ Integer.toString(no + 1));// binary是二进制转换用来区分大小写
		if (al.size() == 0) {
			JOptionPane.showMessageDialog(this, "对不起，【图形组】不存在，请重新输入！");
			// 文本框中的内容自动选中
			return null;
		} else {
			return al.get(0);
		}
	}

	public void mouseClicked(MouseEvent e) {

	}


	public void mouseEntered(MouseEvent e) {
		for (int i = 0; i < num; i++) {
			if (e.getSource() == btn[i]) {
				SystemPicturSqlDao sDao = new SystemPicturSqlDao();
				ArrayList<SystemPictur> al = sDao.getSystemPictur("where no="
						+ Integer.toString(i + 1));// binary是二进制转换用来区分大小写
				if (al.size() == 0) {
					JOptionPane.showMessageDialog(this, "对不起，【图形组】不存在，请重新输入！");
					// 文本框中的内容自动选中
					return;
				} else {
					systemPictur = al.get(0);
				}
				double x = e.getLocationOnScreen().getX();
				double y = e.getLocationOnScreen().getY();
				showPictur = new ShowPicturForm(systemPictur, x + 50, y + 50);

			}
		}
	}


	public void mouseExited(MouseEvent e) {
		for (int i = 0; i < num; i++) {
			if (e.getSource() == btn[i]) {
				showPictur.dispose();
			}
		}

	}


	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}


	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}
}
